/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

#ifndef _SCTS_THREAD_H
#define _SCTS_THREAD_H
#include "SctsPlatformLib.h"
#include "map"


class SctsMutex;
class SctsSemaphore;
class SctsThread;

bool sctsTerminate();

/*****************************************************************
Status logging functions
******************************************************************/
void sctsStatusLogOpen();
void sctsStatusLogClose();
extern "C"{
void sctsStatusLog(char *status,long size=0);
}
void sctsStatusLog(bool success);
void sctsStatusLog(int i);

/******************************************************************
Win32
******************************************************************/

#ifdef __SCTS_PLATFORM_WIN32__
#include "windows.h"

DWORD WINAPI ThreadProc(LPVOID lpParam);

class SctsMutex
{
private:
	HANDLE handle;
public:
	bool Lock(unsigned long timeout=-1);
	bool Unlock();
	SctsMutex(bool initialOwner=false);
	~SctsMutex();
};

class SctsSemaphore
{
private:
	HANDLE handle;
public:
	bool Lock(unsigned long timeout=-1);
	bool Unlock();
	SctsSemaphore(unsigned long initialCount,unsigned long maximumCount);
	~SctsSemaphore();
};

typedef enum
{
	UNKNOWN_THREAD=0,
	SESSION_THREAD,
	SERVER_THREAD
} SctsThreadType;

extern "C" int sctsGetTerminate();

class SctsThread
{
friend DWORD WINAPI ThreadProc(LPVOID lpParam);
friend int sctsGetTerminate();
friend bool sctsTerminate();
/* Data */
private:
	HANDLE handle;
	DWORD id;
	SctsThreadType type;
	bool terminate;
	SctsMutex terminateLock;
	static std::map<int,SctsThread *> threads;
	static SctsMutex threadsLock;
	
protected:
	virtual int Run();
	virtual int Init();
	virtual int Exit();

/* Implementation */
public:
	static int GetThreadCount(SctsThreadType _type=UNKNOWN_THREAD);
	static void Sleep(unsigned int milliseconds);
	static int GetID();
	static int GetProcessID();
	bool Suspend();
	bool Resume();
	void SetTerminate(bool _terminate);
	bool GetTerminate();
	int Create(bool createSuspended=false);
	SctsThreadType GetType();
	SctsThread(SctsThreadType _type=UNKNOWN_THREAD);
	~SctsThread();
};


#endif

#endif
